﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "GridGroupNewDlg.h"
#include <limits>


#include "IUiDocumentViewManager.h"

#include "IUiDocument.h"
#include "TranslatorManager.h"

#include "UiCommonDialog.h"
#include "GmStyleSheetHelper.h"

#include "GridNewUtils.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"


using namespace gcmp;

GridGroupNewDlg::GridGroupNewDlg(QWidget *pParent)
    : QDialog(pParent,Qt::FramelessWindowHint)
{
    m_ui.setupUi(this);
    std::wstring styleSheetStr = GmStyleSheetHelper::Instance()->GetStyleData(L"new_gbmp");
    setStyleSheet(QString::fromStdWString(styleSheetStr));
    m_ui.gbmp_title->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
    m_ui.gbmp_title->setIconVisible(false);
    m_ui.gbmp_title->setMaxButtonVisible(false);
    m_ui.gbmp_title->setMinButtonVisible(false);
    m_ui.gbmp_title->setText(QString::fromStdWString(GBMP_TR(L"批量创建轴网")));
    this->setProperty("use_gbmp_title", true);

    OwnerPtr<OrthogonalGridGroupWidget> orthoGridWidget = NEW_AS_OWNER_PTR(OrthogonalGridGroupWidget, this->m_ui.gridGroupTabWidget);
    OwnerPtr<RadialGridGroupWidget> radioGridWidget = NEW_AS_OWNER_PTR(RadialGridGroupWidget, this->m_ui.gridGroupTabWidget);
    m_ui.gridGroupTabWidget->addTab(TRANSFER_OWNERSHIP_TO_THIRD_PARTY(orthoGridWidget), QString(QString::fromStdWString(GBMP_TR(L"正交轴网"))));
    m_ui.gridGroupTabWidget->addTab(TRANSFER_OWNERSHIP_TO_THIRD_PARTY(radioGridWidget), QString(QString::fromStdWString(GBMP_TR(L"辐射轴网"))));

    m_orthoAnchorPosType = EnOrthogonalGridGroupAnchorPositionType::BottomLeft;
    m_radialAnchorPosType = EnRadialGridGroupAnchorPositionType::Left;
    m_orthoGridGroupIsRotate = false;
    m_radialGridGroupIsRotate = false;
    connect(m_ui.pushButton_OK, SIGNAL(clicked()), this, SLOT(OnOKButtonClick()));
    connect(m_ui.pushButton_Cancel, SIGNAL(clicked()), this, SLOT(OnCancelButtonClick()));
    connect(m_ui.gridGroupTabWidget, SIGNAL(currentChanged(int)), this, SLOT(OnCurrentIndexChanged(int)));
    connect(m_ui.checkBox_Rotate, SIGNAL(stateChanged(int)), this, SLOT(OnCheckBoxChecked()));
    connect(m_ui.comboBox_AnchorPos, SIGNAL(currentIndexChanged(int)), this, SLOT(OnAnchorTypeComboBoxChanged()));
    connect(m_ui.gridGroupTabWidget->widget(0), SIGNAL(lineEditValidStateUpdated(bool)), this, SLOT(OnLineEditValidStateUpdated(bool)));
    connect(m_ui.gridGroupTabWidget->widget(1), SIGNAL(lineEditValidStateUpdated(bool)), this, SLOT(OnLineEditValidStateUpdated(bool)));
    connect(m_ui.gbmp_title, &GmTitleBar::closeRequested, this, &GridGroupNewDlg::reject);
    connect(m_ui.gbmp_title, &GmTitleBar::moveRequested, this, &GridGroupNewDlg::onMoveRequested);
}

void GridGroupNewDlg::OnOKButtonClick()
{
    CommonGridGroupWidget* currTabWidget;
    if (GetGridGroupType() == EnGridGroupType::Orthogonal)
    {
        currTabWidget = static_cast<OrthogonalGridGroupWidget*> (m_ui.gridGroupTabWidget->currentWidget());
    }
    else
    {
        currTabWidget = static_cast<RadialGridGroupWidget*> (m_ui.gridGroupTabWidget->currentWidget());
    }

    DBG_WARN_AND_RETURN_VOID_UNLESS(currTabWidget != nullptr, L"Tab页指针不能为空！",L"GDMPLab",L"2024-03-30");
    currTabWidget->StartDataCollection();
    if (currTabWidget->Validate())
    {
        QDialog::accept();
    }
}

void GridGroupNewDlg::OnCancelButtonClick()
{
    QDialog::reject();
}

OrthogonalGridGroupParam GridGroupNewDlg::GetOrthogonalGridGroupData() const
{
    OrthogonalGridGroupWidget* currTabWidget = static_cast<OrthogonalGridGroupWidget*> (m_ui.gridGroupTabWidget->widget(0));
    DBG_WARN_AND_RETURN_UNLESS(currTabWidget != nullptr, OrthogonalGridGroupParam(), L"获取正交轴网Tab页失败",L"GDMPLab",L"2024-03-30");
    return{ currTabWidget->GetLeftAxisData(), currTabWidget->GetRightAxisData(), m_orthoAnchorPosType, m_orthoGridGroupIsRotate };

}

RadialGridGroupParam GridGroupNewDlg::GetRadialGridGroupData() const
{
    RadialGridGroupWidget* currTabWidget = static_cast<RadialGridGroupWidget*> (m_ui.gridGroupTabWidget->widget(1));
    DBG_WARN_AND_RETURN_UNLESS(currTabWidget != nullptr, RadialGridGroupParam(), L"获取辐射轴网Tab页失败",L"GDMPLab",L"2024-03-30");
    return{ currTabWidget->GetLeftAxisData(), currTabWidget->GetRightAxisData(), m_radialAnchorPosType, currTabWidget->GetInnerRadius(), m_radialGridGroupIsRotate };
}

EnGridGroupType GridGroupNewDlg::GetGridGroupType() const
{
    if (m_ui.gridGroupTabWidget->currentIndex() == 0)
    {
        return EnGridGroupType::Orthogonal;
    }
    else
    {
        return EnGridGroupType::Radial;
    }
}

void GridGroupNewDlg::CollectAnchorPosType()
{

    if (GetGridGroupType() == EnGridGroupType::Orthogonal)
    {
        EnOrthogonalGridGroupAnchorPositionType res;
        switch (m_ui.comboBox_AnchorPos->currentIndex())
        {
        case 0:
            res = EnOrthogonalGridGroupAnchorPositionType::BottomLeft;
            break;
        case 1:
            res = EnOrthogonalGridGroupAnchorPositionType::TopLeft;
            break;
        case 2:
            res = EnOrthogonalGridGroupAnchorPositionType::TopRight;
            break;
        case 3:
            res = EnOrthogonalGridGroupAnchorPositionType::BottomRight;
            break;
        case 4:
            res = EnOrthogonalGridGroupAnchorPositionType::Center;
            break;
        }
        m_orthoAnchorPosType = res;
        return;
    }
    else if (GetGridGroupType() == EnGridGroupType::Radial)
    {
        EnRadialGridGroupAnchorPositionType res;
        switch (m_ui.comboBox_AnchorPos->currentIndex())
        {
        case 0:
            res = EnRadialGridGroupAnchorPositionType::Left;
            break;
        case 1:
            res = EnRadialGridGroupAnchorPositionType::Right;
            break;
        case 2:
            res = EnRadialGridGroupAnchorPositionType::Center;
            break;
        }
        m_radialAnchorPosType = res;
        return;
    }
    else
    {
        DBG_WARN(L"未定义的轴网类型枚举！",L"GDMPLab",L"2024-03-30");
        return;
    }
}

void GridGroupNewDlg::CollectIsRotate()
{
    if (GetGridGroupType() == EnGridGroupType::Orthogonal)
    {
        m_orthoGridGroupIsRotate = m_ui.checkBox_Rotate->isChecked();
    }
    else if (GetGridGroupType() == EnGridGroupType::Radial)
    {
        m_radialGridGroupIsRotate = m_ui.checkBox_Rotate->isChecked();
    }
    else
    {
        DBG_WARN(L"未定义的轴网类型枚举！",L"GDMPLab",L"2024-03-30");
        return;
    }
}

void gcmp::GridGroupNewDlg::OnCurrentIndexChanged(int index)
{
    m_ui.comboBox_AnchorPos->blockSignals(true);
    m_ui.comboBox_AnchorPos->clear();
    switch (index)
    {
    case 0:

        m_ui.comboBox_AnchorPos->insertItems(0, QStringList()
            << QString::fromStdWString(GBMP_TR(L"左下"))
            << QString::fromStdWString(GBMP_TR(L"左上"))
            << QString::fromStdWString(GBMP_TR(L"右上"))
            << QString::fromStdWString(GBMP_TR(L"右下"))
            << QString::fromStdWString(GBMP_TR(L"中心"))
        );
        m_ui.comboBox_AnchorPos->setCurrentIndex(static_cast<int>(m_orthoAnchorPosType));
        m_ui.checkBox_Rotate->setChecked(m_orthoGridGroupIsRotate);
        break;
    case 1:

        m_ui.comboBox_AnchorPos->insertItems(0, QStringList()
            << QString::fromStdWString(GBMP_TR(L"外左"))
            << QString::fromStdWString(GBMP_TR(L"外右"))
            << QString::fromStdWString(GBMP_TR(L"中心"))
        );
        m_ui.comboBox_AnchorPos->setCurrentIndex(static_cast<int>(m_radialAnchorPosType));
        m_ui.checkBox_Rotate->setChecked(m_radialGridGroupIsRotate);
        break;
    default:
        break;
    }
    m_ui.comboBox_AnchorPos->blockSignals(false);
}

void gcmp::GridGroupNewDlg::OnAnchorTypeComboBoxChanged()
{
    CollectAnchorPosType();
}

void gcmp::GridGroupNewDlg::OnCheckBoxChecked()
{
    CollectIsRotate();
}

void gcmp::GridGroupNewDlg::OnLineEditValidStateUpdated(bool valid)
{
    if (sender()->objectName() == "tab_Orthogonal")
    {
        m_ui.gridGroupTabWidget->setTabEnabled(1, valid);
    }
    else if (sender()->objectName() == "tab_Radiation")
    {
        m_ui.gridGroupTabWidget->setTabEnabled(0, valid);
    }
    else
    {
        DBG_WARN(L"未定义的控件",L"GDMPLab",L"2024-03-30");
    }
}

void gcmp::GridGroupNewDlg::onMoveRequested(const QPoint& cursorPos, const QPoint& offset)
{
    Q_UNUSED(cursorPos);
    move(pos() + offset);
}

GridGroupDlgConfigParam::OrthogonalGridGroupDlgConfigParam gcmp::GridGroupNewDlg::GetOrthogGridGroupCommonParam() const
{
    OrthogonalGridGroupWidget* orthoTabWidget = static_cast<OrthogonalGridGroupWidget*> (m_ui.gridGroupTabWidget->widget(0));
    DBG_WARN_AND_RETURN_UNLESS(orthoTabWidget != nullptr, GridGroupDlgConfigParam::OrthogonalGridGroupDlgConfigParam(), L"获取正交轴网Tab页失败",L"GDMPLab",L"2024-03-30");
    return{ orthoTabWidget->GetLeftTableIndex(),orthoTabWidget->GetRightTableIndex(),m_orthoAnchorPosType, m_orthoGridGroupIsRotate };
}

GridGroupDlgConfigParam::RadialGridGroupDlgConfigParam gcmp::GridGroupNewDlg::GetRadialGridGroupCommonParam() const
{
    RadialGridGroupWidget* radialTabWidget = static_cast<RadialGridGroupWidget*> (m_ui.gridGroupTabWidget->widget(1));
    DBG_WARN_AND_RETURN_UNLESS(radialTabWidget != nullptr, GridGroupDlgConfigParam::RadialGridGroupDlgConfigParam(), L"获取辐射轴网Tab页失败",L"GDMPLab",L"2024-03-30");
    return{ radialTabWidget->GetLeftTableIndex(),radialTabWidget->GetRightTableIndex(),radialTabWidget->GetInnerRadius(),m_radialAnchorPosType, m_radialGridGroupIsRotate };
}

gcmp::GridGroupDlgConfigParam gcmp::GridGroupNewDlg::GetConfigData() const
{
    GridGroupDlgConfigParam::OrthogonalGridGroupDlgConfigParam orthoConfigCommonParam = GetOrthogGridGroupCommonParam();
    GridGroupDlgConfigParam::RadialGridGroupDlgConfigParam radialConfigCommonParam = GetRadialGridGroupCommonParam();
    return{ GetGridGroupType(),orthoConfigCommonParam,radialConfigCommonParam };
}

bool gcmp::GridGroupNewDlg::SetConfigData(const GridGroupDlgConfigParam& configParam)
{
    if (configParam.CurrentGridGroupType == EnGridGroupType::Orthogonal)
    {
        m_ui.gridGroupTabWidget->setCurrentIndex(0);
        m_ui.checkBox_Rotate->setChecked(configParam.orthogDlgConfigParam.Rotate);
        m_ui.comboBox_AnchorPos->clear();
        m_ui.comboBox_AnchorPos->insertItems(0, QStringList()
            << QString::fromStdWString(GBMP_TR(L"左下"))
            << QString::fromStdWString(GBMP_TR(L"左上"))
            << QString::fromStdWString(GBMP_TR(L"右上"))
            << QString::fromStdWString(GBMP_TR(L"右下"))
            << QString::fromStdWString(GBMP_TR(L"中心"))
        );
        m_ui.comboBox_AnchorPos->setCurrentIndex(static_cast<int>(configParam.orthogDlgConfigParam.AnchorType));
        m_orthoAnchorPosType = configParam.orthogDlgConfigParam.AnchorType;
        m_radialAnchorPosType = configParam.RadDlgConfigParam.AnchorType;
        m_orthoGridGroupIsRotate = configParam.orthogDlgConfigParam.Rotate;
        m_radialGridGroupIsRotate = configParam.RadDlgConfigParam.Rotate;
    }
    else if (configParam.CurrentGridGroupType == EnGridGroupType::Radial)
    {
        m_ui.gridGroupTabWidget->setCurrentIndex(1);
        m_ui.checkBox_Rotate->setChecked(configParam.RadDlgConfigParam.Rotate);
        m_ui.comboBox_AnchorPos->clear();
        m_ui.comboBox_AnchorPos->insertItems(0, QStringList()
            << QString::fromStdWString(GBMP_TR(L"外左"))
            << QString::fromStdWString(GBMP_TR(L"外右"))
            << QString::fromStdWString(GBMP_TR(L"中心"))
        );
        m_ui.comboBox_AnchorPos->setCurrentIndex(static_cast<int>(configParam.RadDlgConfigParam.AnchorType));
        m_orthoAnchorPosType = configParam.orthogDlgConfigParam.AnchorType;
        m_radialAnchorPosType = configParam.RadDlgConfigParam.AnchorType;
        m_orthoGridGroupIsRotate = configParam.orthogDlgConfigParam.Rotate;
        m_radialGridGroupIsRotate = configParam.RadDlgConfigParam.Rotate;
    }
    else
    {
        DBG_WARN(L"未识别的轴网类型枚举值！",L"GDMPLab",L"2024-03-30");
        return false;
    }
    OrthogonalGridGroupWidget* orthoTabWidget = static_cast<OrthogonalGridGroupWidget*> (m_ui.gridGroupTabWidget->widget(0));
    DBG_WARN_AND_RETURN_UNLESS(orthoTabWidget != nullptr, false, L"获取正交轴网Tab页失败",L"GDMPLab",L"2024-03-30");
    RadialGridGroupWidget* radialTabWidget = static_cast<RadialGridGroupWidget*> (m_ui.gridGroupTabWidget->widget(1));
    DBG_WARN_AND_RETURN_UNLESS(radialTabWidget != nullptr, false, L"获取辐射轴网Tab页失败",L"GDMPLab",L"2024-03-30");
    return orthoTabWidget->InitialConfig(configParam) && radialTabWidget->InitialConfig(configParam);
}

gcmp::CommonGridGroupWidget::CommonGridGroupWidget(QWidget *pParent) 
    : QWidget(pParent)
    , m_rowIndexForJournal(0)
{

}

void gcmp::CommonGridGroupWidget::StartDataCollection()
{
    m_leftAxis.clear();
    m_rightAxis.clear();
    CollectDataFromTable(m_tableWidget_Left, m_leftAxis);
    CollectDataFromTable(m_tableWidget_Right, m_rightAxis);
}


bool gcmp::CommonGridGroupWidget::Validate()
{
    return ValidateDataCollected(m_leftAxis) && ValidateDataCollected(m_rightAxis) && ValidateAndRectifyGridName();
}

gcmp::CommonGridGroupWidget::AxisPositionSet gcmp::CommonGridGroupWidget::GetLeftAxisData() const
{
    return m_leftAxis;
}

gcmp::CommonGridGroupWidget::AxisPositionSet gcmp::CommonGridGroupWidget::GetRightAxisData() const
{
    return m_rightAxis;
}

void gcmp::CommonGridGroupWidget::OnAddUpLeftAxis()
{
    m_leftTableFinalLineClicked = AddAxis(m_tableWidget_Left, m_lineEdit_InputValue_Left, GT_AddUp);
}

void gcmp::CommonGridGroupWidget::OnAddDownLeftAxis()
{
    m_leftTableFinalLineClicked = AddAxis(m_tableWidget_Left, m_lineEdit_InputValue_Left, GT_AddDown);
}

void gcmp::CommonGridGroupWidget::OnDeleteLeftAxis()
{
    m_leftTableFinalLineClicked = DeleteAxis(m_tableWidget_Left);
}

void gcmp::CommonGridGroupWidget::OnAddUpRightAxis()
{
    m_rightTableFinalLineClicked = AddAxis(m_tableWidget_Right, m_lineEdit_InputValue_Right, GT_AddUp);
}

void gcmp::CommonGridGroupWidget::OnAddDownRightAxis()
{
    m_rightTableFinalLineClicked = AddAxis(m_tableWidget_Right, m_lineEdit_InputValue_Right, GT_AddDown);
}

void gcmp::CommonGridGroupWidget::OnDeleteRightAxis()
{
    m_rightTableFinalLineClicked = DeleteAxis(m_tableWidget_Right);
}

void gcmp::CommonGridGroupWidget::OnLeftTableMultiLineClicked()
{
    std::set<int, std::greater<int>> vecItemIndex;
    //获取当前所有选中项
    QList<QTableWidgetItem *> selections = m_tableWidget_Left->selectedItems();
    m_pushButton_AddLineDown_Left->setEnabled(true);
    m_pushButton_AddLineUp_Left->setEnabled(true);
    m_pushButton_DelLine_Left->setEnabled(true);

    //如果未选中任何，则强制选中最后一行数据，以保证表格内始终选中某一行
    if (selections.count() == 0)
    {
        m_tableWidget_Left->selectRow(m_leftTableFinalLineClicked);
        emit tableWidgetLineChanged(m_tableWidget_Left);
    }
    else
    {
        for (int index = 0; index < selections.count(); index++)
        {
            vecItemIndex.insert(selections.at(index)->row());
        }
        m_leftTableFinalLineClicked = *vecItemIndex.cbegin();
    }
    //如果选中项多于一行，则禁用添加按钮，若选中所有行，则禁用删除行按钮
    if (vecItemIndex.size() > 1)
    {
        m_pushButton_AddLineDown_Left->setEnabled(false);
        m_pushButton_AddLineUp_Left->setEnabled(false);
        if (vecItemIndex.size() == m_tableWidget_Left->rowCount())
        {
            m_pushButton_DelLine_Left->setEnabled(false);
        }
    }
}

void gcmp::CommonGridGroupWidget::OnRightTableMultiLineClicked()
{
    std::set<int, std::greater<int>> vecItemIndex;
    QList<QTableWidgetItem *> selections = m_tableWidget_Right->selectedItems();
    m_pushButton_AddLineDown_Right->setEnabled(true);
    m_pushButton_AddLineUp_Right->setEnabled(true);
    m_pushButton_DelLine_Right->setEnabled(true);

    if (selections.count() == 0)
    {
        m_tableWidget_Right->selectRow(m_rightTableFinalLineClicked);
        emit tableWidgetLineChanged(m_tableWidget_Right);
    }
    else
    {
        for (int index = 0; index < selections.count(); index++)
        {
            vecItemIndex.insert(selections.at(index)->row());
        }
        m_rightTableFinalLineClicked = *vecItemIndex.cbegin();
    }

    if (vecItemIndex.size() > 1)
    {
        m_pushButton_AddLineDown_Right->setEnabled(false);
        m_pushButton_AddLineUp_Right->setEnabled(false);
        if (vecItemIndex.size() == m_tableWidget_Right->rowCount())
        {
            m_pushButton_DelLine_Right->setEnabled(false);
        }
    }
}

void gcmp::CommonGridGroupWidget::LeftGridNameBoxChanged(QComboBox* pNameComboBox, int index)
{
    pNameComboBox->clear();
    QStringList pStringList;
    std::vector<std::wstring> tempStringSet;
    switch (index)
    {
    case 0:
        tempStringSet = { L"A",L"B",L"C",L"D",L"E",L"F",L"G",L"H",L"J",L"K",L"L",L"M",L"N",L"P",L"Q",L"R",L"S",L"T",L"U",L"V",L"W",L"X",L"Y" };
        for (int i = 0; i < tempStringSet.size(); i++)
        {
            pStringList << QString::fromStdWString(GBMP_TR(tempStringSet[i]));
        }
        break;
    case 1:
        for (int i = 1; i <= 99; i++)
        {
            pStringList << QString::fromStdWString(GBMP_TR(StringUtil::ToWString(i)));
        }
        break;
    case 2:
        tempStringSet = { L"a",L"b",L"c",L"d",L"e",L"f",L"g",L"h",L"j",L"k",L"l",L"m",L"n",L"p",L"q",L"r",L"s",L"t",L"u",L"v",L"w",L"x",L"y" };
        for (int i = 0; i < tempStringSet.size(); i++)
        {
            pStringList << QString::fromStdWString(GBMP_TR(tempStringSet[i]));
        }
        break;
    }
    pNameComboBox->insertItems(0, pStringList);
}

void gcmp::CommonGridGroupWidget::OnLeftGridNameBoxChanged(int index)
{
    LeftGridNameBoxChanged(m_comboBox_GridName_Left, index);
}

void gcmp::CommonGridGroupWidget::RightGridNameBoxChanged(QComboBox* pNameComboBox, int index)
{
    pNameComboBox->clear();
    QStringList pStringList;
    std::vector<std::wstring> tempStringSet;
    switch (index)
    {
    case 0:
        for (int i = 1; i <= 99; i++)
        {
            pStringList << QString::fromStdWString(GBMP_TR(StringUtil::ToWString(i)));
        }
        break;
    case 1:
        tempStringSet = { L"A",L"B",L"C",L"D",L"E",L"F",L"G",L"H",L"J",L"K",L"L",L"M",L"N",L"P",L"Q",L"R",L"S",L"T",L"U",L"V",L"W",L"X",L"Y" };
        for (int i = 0; i < tempStringSet.size(); i++)
        {
            pStringList << QString::fromStdWString(GBMP_TR(tempStringSet[i]));
        }
        break;
    case 2:
        tempStringSet = { L"a",L"b",L"c",L"d",L"e",L"f",L"g",L"h",L"j",L"k",L"l",L"m",L"n",L"p",L"q",L"r",L"s",L"t",L"u",L"v",L"w",L"x",L"y" };
        for (int i = 0; i < tempStringSet.size(); i++)
        {
            pStringList << QString::fromStdWString(GBMP_TR(tempStringSet[i]));
        }
        break;
    }
    pNameComboBox->insertItems(0, pStringList);
}

void gcmp::CommonGridGroupWidget::OnRightGridNameBoxChanged(int index)
{
    RightGridNameBoxChanged(m_comboBox_GridName_Right, index);
}

void gcmp::CommonGridGroupWidget::EnableDelLineBtn(QTableWidget *currTableWidget)
{
    QPushButton *pPushButton;
    if (currTableWidget->objectName() == "tableWidget_Right")
    {
        pPushButton = m_pushButton_DelLine_Right;
    }
    else if (currTableWidget->objectName() == "tableWidget_Left")
    {
        pPushButton = m_pushButton_DelLine_Left;
    }
    else
    {
        DBG_WARN(L"未定义的控件",L"GDMPLab",L"2024-03-30");
        return;
    }
    pPushButton->setEnabled(currTableWidget->rowCount() == 1 ? false : true);
}

void gcmp::CommonGridGroupWidget::OnValidStateUpdated(QLineEdit* pEdit, bool valid)
{
    if (pEdit->objectName() == "lineEdit_InputValue_Left")
    {
        m_pushButton_AddLineUp_Left->setEnabled(valid);
        m_pushButton_AddLineDown_Left->setEnabled(valid);
        m_isLeftInputValid = valid;
    }
    else if (pEdit->objectName() == "lineEdit_InputValue_Right")
    {
        m_pushButton_AddLineUp_Right->setEnabled(valid);
        m_pushButton_AddLineDown_Right->setEnabled(valid);
        m_isRightInputValid = valid;
    }
    else if (pEdit->objectName() == "lineEdit_InnerRadius")
    {
        m_isRadiusInputValid = valid;
    }
    emit lineEditValidStateUpdated(valid);
}

int gcmp::CommonGridGroupWidget::AddAxis(QTableWidget *pTableWidget, QLineEdit *pQLineEdit, EnLineAddType pLineAddType)
{
    int currentRow = pTableWidget->currentRow();
    int nNewRow = 0;
    if (currentRow == -1)
    {
        nNewRow = pTableWidget->rowCount();
    }
    switch (pLineAddType)
    {
    case GT_AddUp:
        nNewRow = currentRow;
        break;
    case GT_AddDown:
        nNewRow = currentRow + 1;
        break;
    default:
        break;
    }

    pTableWidget->insertRow(nNewRow);
    OwnerPtr<QTableWidgetItem> item = NEW_AS_OWNER_PTR(QTableWidgetItem, pQLineEdit->text());
    pTableWidget->setItem(nNewRow, 0, TRANSFER_OWNERSHIP_TO_THIRD_PARTY(item));
    item = NEW_AS_OWNER_PTR(QTableWidgetItem, QString("1"));
    pTableWidget->setItem(nNewRow, 1, TRANSFER_OWNERSHIP_TO_THIRD_PARTY(item));
    pTableWidget->selectRow(nNewRow);

    // 支持日志
    OwnerPtr<QTableWidgetItem> opVerticalHeaderItem = NEW_AS_OWNER_PTR(QTableWidgetItem);
    pTableWidget->setVerticalHeaderItem(nNewRow, TRANSFER_OWNERSHIP_TO_THIRD_PARTY(opVerticalHeaderItem));
    pTableWidget->model()->setHeaderData(nNewRow, Qt::Vertical, QString::number(m_rowIndexForJournal++), Qt::AccessibleDescriptionRole);

    emit tableWidgetLineChanged(pTableWidget);
    return nNewRow;
}

void gcmp::CommonGridGroupWidget::InitWidgetSettings()
{
    m_tableWidget_Left->setSelectionBehavior(QAbstractItemView::SelectRows);
    m_tableWidget_Right->setSelectionBehavior(QAbstractItemView::SelectRows);
    m_tableWidget_Left->horizontalHeader()->setHighlightSections(false);
    m_tableWidget_Right->horizontalHeader()->setHighlightSections(false);
    m_tableWidget_Left->horizontalHeader()->setVisible(true);
    m_tableWidget_Right->horizontalHeader()->setVisible(true);

    if (m_tableWidget_Left->rowCount() == 1)
    {
        m_pushButton_DelLine_Left->setEnabled(false);
    }

    if (m_tableWidget_Right->rowCount() == 1)
    {
        m_pushButton_DelLine_Right->setEnabled(false);
    }

    m_tableWidget_Left->selectRow(m_tableWidget_Left->rowCount() - 1);
    m_tableWidget_Right->selectRow(m_tableWidget_Right->rowCount() - 1);
    m_leftTableFinalLineClicked = m_tableWidget_Left->rowCount() - 1;
    m_rightTableFinalLineClicked = m_tableWidget_Right->rowCount() - 1;
    //初始化QlineEdit输入验证器
    InitValidators();

    connect(m_pushButton_AddLineUp_Left, SIGNAL(clicked()), this, SLOT(OnAddUpLeftAxis()));
    connect(m_pushButton_AddLineDown_Left, SIGNAL(clicked()), this, SLOT(OnAddDownLeftAxis()));
    connect(m_pushButton_DelLine_Left, SIGNAL(clicked()), this, SLOT(OnDeleteLeftAxis()));
    connect(m_pushButton_AddLineUp_Right, SIGNAL(clicked()), this, SLOT(OnAddUpRightAxis()));
    connect(m_pushButton_AddLineDown_Right, SIGNAL(clicked()), this, SLOT(OnAddDownRightAxis()));
    connect(m_pushButton_DelLine_Right, SIGNAL(clicked()), this, SLOT(OnDeleteRightAxis()));
    connect(m_comboBox_GridType_Left, SIGNAL(currentIndexChanged(int)), this, SLOT(OnLeftGridNameBoxChanged(int)));
    connect(m_comboBox_GridType_Right, SIGNAL(currentIndexChanged(int)), this, SLOT(OnRightGridNameBoxChanged(int)));
    connect(m_tableWidget_Left, SIGNAL(itemSelectionChanged()), this, SLOT(OnLeftTableMultiLineClicked()));
    connect(m_tableWidget_Right, SIGNAL(itemSelectionChanged()), this, SLOT(OnRightTableMultiLineClicked()));
    connect(this, SIGNAL(tableWidgetLineChanged(QTableWidget *)), this, SLOT(EnableDelLineBtn(QTableWidget *)));
    connect(m_opValidateHelper.get(), SIGNAL(validStateUpdated(QLineEdit*, bool)), this, SLOT(OnValidStateUpdated(QLineEdit*, bool)));
}

int gcmp::CommonGridGroupWidget::DeleteAxis(QTableWidget *currTableWidget)
{
    std::set<int, std::greater<int>> vecItemIndex;

    //获得当前选中的所有单元格
    QList<QTableWidgetItem *> selections = currTableWidget->selectedItems();
    int delRow = currTableWidget->rowCount() - 1;

    //将选中的所有行号按从大到小进行排序
    for (int index = 0; index < selections.count(); index++)
    {
        vecItemIndex.insert(selections.at(index)->row());
    }

    //从后向前依次删除表格中对应行号的数据
    FOR_EACH(itemIndex, vecItemIndex)
    {
        delRow = itemIndex;
        currTableWidget->removeRow(delRow);
    }

    // 新的选中行默认为剩下的第一行
    int nNewRow = 0;
    if (delRow != 0)
    {
        nNewRow = delRow - 1;
    }
    currTableWidget->selectRow(nNewRow);
    emit tableWidgetLineChanged(currTableWidget);
    return nNewRow;
}

void gcmp::CommonGridGroupWidget::CollectDataFromTable(QTableWidget *pTableWidget, AxisPositionSet& axises)
{
    //获取各轴网间距
    std::vector<double> positions;
    double currentPosition = 0.0;
    positions.push_back(currentPosition);

    QTableWidgetItem *item = nullptr;
    int nRowCount = pTableWidget->rowCount();
    for (int row = 0; row < nRowCount; ++row)
    {
        item = pTableWidget->item(row, 0);
        double val = item->text().toDouble();
        item = pTableWidget->item(row, 1);
        int count = item->text().toInt();
        if (count <= 0)
        {
            return;
        }
        for (int i = 1; i <= count; ++i)
        {
            currentPosition += val;
            positions.push_back(currentPosition);
        }
    }

    //获取批量轴网初始轴号
    std::set<std::pair<std::wstring, double>> duplicatedNameSet;
    std::wstring startName;
    if (pTableWidget->objectName() == "tableWidget_Left")
    {
        startName = m_comboBox_GridName_Left->currentText().toStdWString();
    }
    else if (pTableWidget->objectName() == "tableWidget_Right")
    {
        startName = m_comboBox_GridName_Right->currentText().toStdWString();
    }
    else
    {
        DBG_WARN(L"未识别的控件名称",L"GDMPLab",L"2024-03-30");
        return;
    }
    axises.push_back(std::pair<std::wstring, double>(ComposeName(pTableWidget, startName), positions[0]));

    //生成其余轴网轴号
    std::wstring currentName;
    std::wstring preName = startName;
    for (int i = 1; i < (int)positions.size(); ++i)
    {
        if (GridNewUtils::GetNextName(preName, currentName))
        {
            axises.push_back(std::pair<std::wstring, double>(ComposeName(pTableWidget, currentName), positions[i]));
            preName = currentName;
        }
        else
        {
            DBG_WARN_AND_RETURN_VOID_UNLESS(preName != currentName, L"轴号推导出现错误!",L"GDMPLab",L"2024-03-30");
        }
    }
}

std::wstring gcmp::CommonGridGroupWidget::ComposeName(QTableWidget *pTableWidget, const std::wstring &currName) const
{
    if (pTableWidget->objectName() == "tableWidget_Left")
    {
        std::wstring prefixName = m_lineEdit_ExGridName_Left->text().toStdWString();
        if (m_comboBox_ExGridName_Left->currentIndex() == 0)
        {
            return prefixName + currName;
        }
        else
        {
            return currName + prefixName;
        }
    }
    else if (pTableWidget->objectName() == "tableWidget_Right")
    {
        std::wstring prefixName = m_lineEdit_ExGridName_Right->text().toStdWString();
        if (m_comboBox_ExGridName_Right->currentIndex() == 0)
        {
            return prefixName + currName;
        }
        else
        {
            return currName + prefixName;
        }
    }
    else
    {
        DBG_WARN(L"未识别的控件名称",L"GDMPLab",L"2024-03-30");
        return std::wstring();
    }
}

std::wstring gcmp::CommonGridGroupWidget::GetNoDuplicatedName(const std::wstring& currName) const
{
    IUiDocument* pUiDoc = IUiDocumentViewManager::Get()->GetCurrentUiDocument();
    DBG_WARN_AND_RETURN_UNLESS(pUiDoc != nullptr, currName, L"当前文档不能为空!",L"GDMPLab",L"2024-03-30");
    IDocument *pDoc = pUiDoc->GetDbDocument();
    int exName = 1;
    std::wstring resName;
    do
    {
        resName = currName + L"-" + StringUtil::ToWString(exName++);
    } while (GridNewUtils::IsNameDuplicated(pDoc, resName));

    return resName;
}


bool gcmp::CommonGridGroupWidget::InitLeftAndRightTableWidgets(const std::vector<std::pair<std::wstring, std::wstring>> &leftAxisSet, const std::vector<std::pair<std::wstring, std::wstring>> &rightAxisSet)
{
    return InitTableWidget(m_tableWidget_Left, leftAxisSet) && InitTableWidget(m_tableWidget_Right, rightAxisSet);
}

bool gcmp::CommonGridGroupWidget::InitTableWidget(QTableWidget* pTableWidget, const std::vector<std::pair<std::wstring, std::wstring>> &axisSet)
{
    DBG_WARN_AND_RETURN_UNLESS(axisSet.size() != 0, false, L"轴网定位数据为空！",L"GDMPLab",L"2024-03-30");
    for (int i = 0; i < axisSet.size(); i++)
    {
        pTableWidget->insertRow(i);
        OwnerPtr<QTableWidgetItem> item = NEW_AS_OWNER_PTR(QTableWidgetItem, QString::fromStdWString(axisSet[i].first));
        pTableWidget->setItem(i, 0, TRANSFER_OWNERSHIP_TO_THIRD_PARTY(item));
        item = NEW_AS_OWNER_PTR(QTableWidgetItem, QString::fromStdWString(axisSet[i].second));
        pTableWidget->setItem(i, 1, TRANSFER_OWNERSHIP_TO_THIRD_PARTY(item));

        // 支持日志
        OwnerPtr<QTableWidgetItem> opVerticalHeaderItem = NEW_AS_OWNER_PTR(QTableWidgetItem);
        pTableWidget->setVerticalHeaderItem(i, TRANSFER_OWNERSHIP_TO_THIRD_PARTY(opVerticalHeaderItem));
        pTableWidget->model()->setHeaderData(i, Qt::Vertical, QString::number(m_rowIndexForJournal++), Qt::AccessibleDescriptionRole);
    }
    return true;
}



void gcmp::CommonGridGroupWidget::InitValidators()
{
    
    m_opValidateHelper = NEW_AS_OWNER_PTR(LineEditValidateHelper, this);
    m_lenValidator = NumberValidator<int>(0, (std::numeric_limits<int>::max)(), GBMP_TR(L"请输入正整数！"), false, false);
    m_validators.insert(std::make_pair(m_lineEdit_InputValue_Left, &m_lenValidator));
    m_validators.insert(std::make_pair(m_lineEdit_InputValue_Right, &m_lenValidator));
    m_opValidateHelper->SetValidators(m_validators);
}

GridGroupDlgConfigParam::GridGroupDlgConfigCommonParam gcmp::CommonGridGroupWidget::GetLeftTableIndex() const
{
    std::vector<std::pair<std::wstring, std::wstring>> tableValueStr;
    for (int i = 0; i < m_tableWidget_Left->rowCount(); i++)
    {
        tableValueStr.push_back(std::pair<std::wstring, std::wstring>(m_tableWidget_Left->item(i, 0)->text().toStdWString(), m_tableWidget_Left->item(i, 1)->text().toStdWString()));
    }
    return{ m_isLeftInputValid ? (m_lineEdit_InputValue_Left->text()).toStdWString() : m_leftInputValue.toStdWString(),m_comboBox_GridType_Left->currentIndex(),m_comboBox_GridName_Left->currentIndex(),
        m_comboBox_ExGridName_Left->currentIndex(),(m_lineEdit_ExGridName_Left->text()).toStdWString(),tableValueStr };
}

GridGroupDlgConfigParam::GridGroupDlgConfigCommonParam gcmp::CommonGridGroupWidget::GetRightTableIndex() const
{
    std::vector<std::pair<std::wstring, std::wstring>> tableValueStr;
    for (int i = 0; i < m_tableWidget_Right->rowCount(); i++)
    {
        tableValueStr.push_back(std::pair<std::wstring, std::wstring>(m_tableWidget_Right->item(i, 0)->text().toStdWString(), m_tableWidget_Right->item(i, 1)->text().toStdWString()));
    }
    return{ m_isRightInputValid ? (m_lineEdit_InputValue_Right->text()).toStdWString() : m_rightInputValue.toStdWString(), m_comboBox_GridType_Right->currentIndex(),m_comboBox_GridName_Right->currentIndex(),
        m_comboBox_ExGridName_Right->currentIndex(),(m_lineEdit_ExGridName_Right->text()).toStdWString(),tableValueStr };
}

bool gcmp::CommonGridGroupWidget::ValidateAndRectifyGridName()
{
    DBG_WARN_AND_RETURN_UNLESS(!m_leftAxis.empty() && !m_rightAxis.empty(), false, L"轴网数据获取失败，请检查",L"GDMPLab",L"2024-03-30");

    //检查已设定的轴网名称是否重复
    FOR_EACH(m_leftAxisData, m_leftAxis)
    {
        FOR_EACH(m_rightAxisData, m_rightAxis)
        {
            if (m_rightAxisData.first == m_leftAxisData.first)
            {
                UiCommonDialog::ShowMessageBox(GBMP_TR(L"提示信息"), GBMP_TR(L"已设定的轴线轴号存在重名，请修改。"), (int)UiCommonDialog::ButtonType::OK);
                return false;
            }
        }
    }

    //检查是否与项目中已有轴号重名
    IUiDocument* pUiDoc = IUiDocumentViewManager::Get()->GetCurrentUiDocument();
    DBG_WARN_AND_RETURN_UNLESS(pUiDoc != nullptr, false, L"当前文档不能为空!",L"GDMPLab",L"2024-03-30");
    IDocument *pDoc = pUiDoc->GetDbDocument();
    std::set<std::wstring> duplicatedSet;
    FOR_EACH(m_leftAxisData, m_leftAxis)
    {
        if (GridNewUtils::IsNameDuplicated(pDoc, m_leftAxisData.first))
        {
            duplicatedSet.insert(m_leftAxisData.first);
        }
    }
    FOR_EACH(m_rightAxisData, m_rightAxis)
    {
        if (GridNewUtils::IsNameDuplicated(pDoc, m_rightAxisData.first))
        {
            duplicatedSet.insert(m_rightAxisData.first);
        }
    }


    if (!duplicatedSet.empty())
    {
        std::wstring duplicateNameSetStr = GBMP_TR(L"重名轴号：");
        FOR_EACH(duplicateName, duplicatedSet)
        {
            duplicateNameSetStr += duplicateName + L",";
        }
        UiCommonDialog::ButtonType btn = UiCommonDialog::ShowMessageBox(GBMP_TR(L"提示信息"), GBMP_TR(L"检测到如下重名的轴号，继续将以数字后缀重命名即将创建的轴线？\n\n" + duplicateNameSetStr),
            (int)UiCommonDialog::ButtonType::OK | (int)UiCommonDialog::ButtonType::No);

        //对与项目中已有轴号重名的轴号，添加后缀重新生成
        AxisPositionSet tempPositionSet;
        if (btn == UiCommonDialog::ButtonType::OK)
        {
            FOR_EACH(leftAxisData, m_leftAxis)
            {
                if (GridNewUtils::IsNameDuplicated(pDoc, leftAxisData.first))
                {
                    tempPositionSet.push_back(std::pair<std::wstring, double>(GetNoDuplicatedName(leftAxisData.first), leftAxisData.second));
                }
                else
                {
                    tempPositionSet.push_back(std::pair<std::wstring, double>(leftAxisData.first, leftAxisData.second));
                }
            }
            m_leftAxis = tempPositionSet;

            tempPositionSet.clear();
            FOR_EACH(rightAxisData, m_rightAxis)
            {
                if (GridNewUtils::IsNameDuplicated(pDoc, rightAxisData.first))
                {
                    tempPositionSet.push_back(std::pair<std::wstring, double>(GetNoDuplicatedName(rightAxisData.first), rightAxisData.second));
                }
                else
                {
                    tempPositionSet.push_back(std::pair<std::wstring, double>(rightAxisData.first, rightAxisData.second));
                }
            }
            m_rightAxis = tempPositionSet;
        }
        else
        {
            return false;
        }
    }
    return true;
}

bool gcmp::CommonGridGroupWidget::ValidateDataCollected(const AxisPositionSet &axisSet) const
{
    FOR_EACH(axisSetData, axisSet)
    {
        if (!GridNewUtils::IsNameValid(axisSetData.first))
        {
            UiCommonDialog::ShowMessageBox(GBMP_TR(L"提示信息"), GBMP_TR(L"轴号名称存在非法字符，请重新输入。"), (int)UiCommonDialog::ButtonType::OK);
            return false;
        }
    }
    return true;
}

gcmp::OrthogonalGridGroupWidget::OrthogonalGridGroupWidget(QWidget *pParent) :CommonGridGroupWidget(pParent)
{
    m_orthoTab.setupUi(this);
    m_pushButton_AddLineUp_Left = m_orthoTab.pushButton_AddLineUp_Left;
    m_pushButton_AddLineDown_Left = m_orthoTab.pushButton_AddLineDown_Left;
    m_comboBox_GridName_Left = m_orthoTab.comboBox_GridName_Left;
    m_lineEdit_ExGridName_Left = m_orthoTab.lineEdit_ExGridName_Left;
    m_comboBox_ExGridName_Left = m_orthoTab.comboBox_ExGridName_Left;
    m_tableWidget_Left = m_orthoTab.tableWidget_Left;
    m_comboBox_GridType_Left = m_orthoTab.comboBox_GridType_Left;
    m_lineEdit_InputValue_Left = m_orthoTab.lineEdit_InputValue_Left;
    m_pushButton_DelLine_Left = m_orthoTab.pushButton_DelLine_Left;
    m_pushButton_AddLineUp_Right = m_orthoTab.pushButton_AddLineUp_Right;
    m_pushButton_AddLineDown_Right = m_orthoTab.pushButton_AddLineDown_Right;
    m_comboBox_GridName_Right = m_orthoTab.comboBox_GridName_Right;
    m_lineEdit_ExGridName_Right = m_orthoTab.lineEdit_ExGridName_Right;
    m_comboBox_ExGridName_Right = m_orthoTab.comboBox_ExGridName_Right;
    m_tableWidget_Right = m_orthoTab.tableWidget_Right;
    m_comboBox_GridType_Right = m_orthoTab.comboBox_GridType_Right;
    m_lineEdit_InputValue_Right = m_orthoTab.lineEdit_InputValue_Right;
    m_pushButton_DelLine_Right = m_orthoTab.pushButton_DelLine_Right;

}

bool gcmp::OrthogonalGridGroupWidget::InitialConfig(const GridGroupDlgConfigParam& initParam)
{
    m_lineEdit_InputValue_Left->setText(QString::fromStdWString(initParam.orthogDlgConfigParam.LeftParam.AddInputValue));
    m_lineEdit_InputValue_Right->setText(QString::fromStdWString(initParam.orthogDlgConfigParam.RightParam.AddInputValue));

    m_leftInputValue = QString::fromStdWString(initParam.orthogDlgConfigParam.LeftParam.AddInputValue);
    m_rightInputValue = QString::fromStdWString(initParam.orthogDlgConfigParam.RightParam.AddInputValue);

    m_comboBox_GridType_Left->setCurrentIndex(initParam.orthogDlgConfigParam.LeftParam.SerialNumTypeIndex);
    OnLeftGridNameBoxChanged(initParam.orthogDlgConfigParam.LeftParam.SerialNumTypeIndex);
    m_comboBox_GridType_Right->setCurrentIndex(initParam.orthogDlgConfigParam.RightParam.SerialNumTypeIndex);
    OnRightGridNameBoxChanged(initParam.orthogDlgConfigParam.RightParam.SerialNumTypeIndex);
    m_comboBox_GridName_Left->setCurrentIndex(initParam.orthogDlgConfigParam.LeftParam.SerialNumStartIndex);
    m_comboBox_GridName_Right->setCurrentIndex(initParam.orthogDlgConfigParam.RightParam.SerialNumStartIndex);
    m_comboBox_ExGridName_Left->setCurrentIndex(initParam.orthogDlgConfigParam.LeftParam.SerialNumAuxTypeIndex);
    m_comboBox_ExGridName_Right->setCurrentIndex(initParam.orthogDlgConfigParam.RightParam.SerialNumAuxTypeIndex);
    m_lineEdit_ExGridName_Left->setText(QString::fromStdWString(initParam.orthogDlgConfigParam.LeftParam.SerialNumAuxStr));
    m_lineEdit_ExGridName_Right->setText(QString::fromStdWString(initParam.orthogDlgConfigParam.RightParam.SerialNumAuxStr));
    // 支持日志
    m_tableWidget_Left->model()->setHeaderData(0, Qt::Horizontal, QString::fromStdWString(GBMP_TR(L"横向轴网-轴间距")), Qt::AccessibleDescriptionRole);
    m_tableWidget_Left->model()->setHeaderData(1, Qt::Horizontal, QString::fromStdWString(GBMP_TR(L"横向轴网-个数")), Qt::AccessibleDescriptionRole);

    m_tableWidget_Right->model()->setHeaderData(0, Qt::Horizontal, QString::fromStdWString(GBMP_TR(L"纵向轴网-轴间距")), Qt::AccessibleDescriptionRole);
    m_tableWidget_Right->model()->setHeaderData(1, Qt::Horizontal, QString::fromStdWString(GBMP_TR(L"纵向轴网-个数")), Qt::AccessibleDescriptionRole);

    bool res = InitLeftAndRightTableWidgets(initParam.orthogDlgConfigParam.LeftParam.TableWidgetValue, initParam.orthogDlgConfigParam.RightParam.TableWidgetValue);
    InitWidgetSettings();
    m_tableWidgetDelegate = NEW_AS_OWNER_PTR(LineEditDelegate, this);
    m_tableWidget_Left->setItemDelegate(m_tableWidgetDelegate.get());
    m_tableWidget_Right->setItemDelegate(m_tableWidgetDelegate.get());
    return res;
}


gcmp::RadialGridGroupWidget::RadialGridGroupWidget(QWidget *pParent) :CommonGridGroupWidget(pParent)
{
    m_radialTab.setupUi(this);
    m_pushButton_AddLineUp_Left = m_radialTab.pushButton_AddLineUp_Left;
    m_pushButton_AddLineDown_Left = m_radialTab.pushButton_AddLineDown_Left;
    m_comboBox_GridName_Left = m_radialTab.comboBox_GridName_Left;
    m_lineEdit_ExGridName_Left = m_radialTab.lineEdit_ExGridName_Left;
    m_comboBox_ExGridName_Left = m_radialTab.comboBox_ExGridName_Left;
    m_tableWidget_Left = m_radialTab.tableWidget_Left;
    m_comboBox_GridType_Left = m_radialTab.comboBox_GridType_Left;
    m_lineEdit_InputValue_Left = m_radialTab.lineEdit_InputValue_Left;
    m_pushButton_DelLine_Left = m_radialTab.pushButton_DelLine_Left;
    m_pushButton_AddLineUp_Right = m_radialTab.pushButton_AddLineUp_Right;
    m_pushButton_AddLineDown_Right = m_radialTab.pushButton_AddLineDown_Right;
    m_comboBox_GridName_Right = m_radialTab.comboBox_GridName_Right;
    m_lineEdit_ExGridName_Right = m_radialTab.lineEdit_ExGridName_Right;
    m_comboBox_ExGridName_Right = m_radialTab.comboBox_ExGridName_Right;
    m_tableWidget_Right = m_radialTab.tableWidget_Right;
    m_comboBox_GridType_Right = m_radialTab.comboBox_GridType_Right;
    m_lineEdit_InputValue_Right = m_radialTab.lineEdit_InputValue_Right;
    m_pushButton_DelLine_Right = m_radialTab.pushButton_DelLine_Right;
}


bool gcmp::RadialGridGroupWidget::InitialConfig(const GridGroupDlgConfigParam& initParam)
{
    m_lineEdit_InputValue_Left->setText(QString::fromStdWString(initParam.RadDlgConfigParam.LeftParam.AddInputValue));
    m_lineEdit_InputValue_Right->setText(QString::fromStdWString(initParam.RadDlgConfigParam.RightParam.AddInputValue));

    m_leftInputValue = QString::fromStdWString(initParam.RadDlgConfigParam.LeftParam.AddInputValue);
    m_rightInputValue = QString::fromStdWString(initParam.RadDlgConfigParam.RightParam.AddInputValue);

    m_comboBox_GridType_Left->setCurrentIndex(initParam.RadDlgConfigParam.LeftParam.SerialNumTypeIndex);
    OnLeftGridNameBoxChanged(initParam.RadDlgConfigParam.LeftParam.SerialNumTypeIndex);
    m_comboBox_GridType_Right->setCurrentIndex(initParam.RadDlgConfigParam.RightParam.SerialNumTypeIndex);
    OnRightGridNameBoxChanged(initParam.RadDlgConfigParam.RightParam.SerialNumTypeIndex);
    m_comboBox_GridName_Left->setCurrentIndex(initParam.RadDlgConfigParam.LeftParam.SerialNumStartIndex);
    m_comboBox_GridName_Right->setCurrentIndex(initParam.RadDlgConfigParam.RightParam.SerialNumStartIndex);
    m_comboBox_ExGridName_Left->setCurrentIndex(initParam.RadDlgConfigParam.LeftParam.SerialNumAuxTypeIndex);
    m_comboBox_ExGridName_Right->setCurrentIndex(initParam.RadDlgConfigParam.RightParam.SerialNumAuxTypeIndex);
    m_lineEdit_ExGridName_Left->setText(QString::fromStdWString(initParam.RadDlgConfigParam.LeftParam.SerialNumAuxStr));
    m_lineEdit_ExGridName_Right->setText(QString::fromStdWString(initParam.RadDlgConfigParam.RightParam.SerialNumAuxStr));
    m_radialTab.lineEdit_InnerRadius->setText(QString::number(initParam.RadDlgConfigParam.InnerRadius));
    m_innerRadiusValue = initParam.RadDlgConfigParam.InnerRadius;

    // 支持日志
    m_tableWidget_Left->model()->setHeaderData(0, Qt::Horizontal, QString::fromStdWString(GBMP_TR(L"径向轴网-轴间距")), Qt::AccessibleDescriptionRole);
    m_tableWidget_Left->model()->setHeaderData(1, Qt::Horizontal, QString::fromStdWString(GBMP_TR(L"径向轴网-个数")), Qt::AccessibleDescriptionRole);

    m_tableWidget_Right->model()->setHeaderData(0, Qt::Horizontal, QString::fromStdWString(GBMP_TR(L"环向轴网-轴间距")), Qt::AccessibleDescriptionRole);
    m_tableWidget_Right->model()->setHeaderData(1, Qt::Horizontal, QString::fromStdWString(GBMP_TR(L"环向轴网-个数")), Qt::AccessibleDescriptionRole);

    bool res = InitLeftAndRightTableWidgets(initParam.RadDlgConfigParam.LeftParam.TableWidgetValue, initParam.RadDlgConfigParam.RightParam.TableWidgetValue);
    InitWidgetSettings();

    m_tableWidgetDelegate = NEW_AS_OWNER_PTR(LineEditDelegate, this);
    m_angleDelegate = NEW_AS_OWNER_PTR(RadialTableWidgetLineEditDelegate, this);
    m_tableWidget_Left->setItemDelegate(m_angleDelegate.get());
    m_tableWidget_Right->setItemDelegate(m_tableWidgetDelegate.get());
    return res;
}


void gcmp::RadialGridGroupWidget::InitValidators()
{
    m_opValidateHelper = NEW_AS_OWNER_PTR(LineEditValidateHelper, this);
    m_lenValidator = NumberValidator<int>(0, (std::numeric_limits<int>::max)(), GBMP_TR(L"请输入正整数！"), false, false);
    m_angleValidator = NumberValidator<double>(0, (std::numeric_limits<double>::max)(), GBMP_TR(L"请输入正数！"), false, false);
    m_validators.insert(std::make_pair(m_lineEdit_InputValue_Left, &m_angleValidator));
    m_validators.insert(std::make_pair(m_lineEdit_InputValue_Right, &m_lenValidator));
    m_validators.insert(std::make_pair(m_radialTab.lineEdit_InnerRadius, &m_lenValidator));
    m_opValidateHelper->SetValidators(m_validators);
}

bool gcmp::RadialGridGroupWidget::Validate()
{
    return ValidateDataCollected(m_leftAxis) && CommonGridGroupWidget::ValidateDataCollected(m_rightAxis) && ValidateAndRectifyGridName();
}

bool gcmp::RadialGridGroupWidget::ValidateDataCollected(const AxisPositionSet &axisSet) const
{
    FOR_EACH(axisSetValue, axisSet)
    {
        if (!MathUtils::IsLessThan(axisSetValue.second, 300, Constants::ANGLE_EPS))
        {
            UiCommonDialog::ShowMessageBox(GBMP_TR(L"提示信息"), GBMP_TR(L"径向轴线轴间角之和不允许大于等于300°，请修改输入信息。"), (int)UiCommonDialog::ButtonType::OK);
            return false;
        }
    }
    return CommonGridGroupWidget::ValidateDataCollected(axisSet);
}

void gcmp::RadialGridGroupWidget::OnLeftGridNameBoxChanged(int index)
{
    RightGridNameBoxChanged(m_comboBox_GridName_Left, index);
}

void gcmp::RadialGridGroupWidget::OnRightGridNameBoxChanged(int index)
{
    LeftGridNameBoxChanged(m_comboBox_GridName_Right, index);
}

gcmp::LineEditDelegate::LineEditDelegate(QObject *parent /*= 0*/) :QItemDelegate(parent)
{
    m_lenValidator = NumberValidator<int>(0, (std::numeric_limits<int>::max)(), GBMP_TR(L"请输入正整数！"), false, false);
    m_opValidateHelper = NEW_AS_OWNER_PTR(LineEditValidateHelper, parent);
    connect(m_opValidateHelper.get(), SIGNAL(validStateUpdated(QLineEdit*, bool)), this, SLOT(OnValidStateUpdated(QLineEdit*, bool)));
}

QWidget * gcmp::LineEditDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    m_validators.clear();
    m_tableWidgetLineEdit = NEW_AS_OWNER_PTR(QLineEdit, parent);
    m_validators.insert(std::make_pair(m_tableWidgetLineEdit.get(), &m_lenValidator));
    m_opValidateHelper->SetValidators(m_validators);
    return TRANSFER_OWNERSHIP_TO_THIRD_PARTY(m_tableWidgetLineEdit);
}

void gcmp::LineEditDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    QString value = index.model()->data(index, Qt::EditRole).toString();
    QLineEdit* pLineEdit = static_cast<QLineEdit*>(editor);
    pLineEdit->setText(value);
    m_lastValidData = value;
}

void gcmp::LineEditDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    QLineEdit* pLineEdit = static_cast<QLineEdit*>(editor);
    if (m_isValid)
    {
        model->setData(index, pLineEdit->text(), Qt::EditRole);
    }
    else
    {
        model->setData(index, m_lastValidData, Qt::EditRole);
    }
}

void gcmp::LineEditDelegate::OnValidStateUpdated(QLineEdit* pEdit, bool valid)
{
    m_isValid = valid;
}

gcmp::RadialTableWidgetLineEditDelegate::RadialTableWidgetLineEditDelegate(QObject *parent /*= 0*/) :LineEditDelegate(parent)
{
    m_angleValidator = NumberValidator<double>(0, (std::numeric_limits<double>::max)(), GBMP_TR(L"请输入正数！"), false, false);
}

QWidget * gcmp::RadialTableWidgetLineEditDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    m_validators.clear();
    m_tableWidgetLineEdit = NEW_AS_OWNER_PTR(QLineEdit, parent);
    if (index.column() == 0)
    {
        m_validators.insert(std::make_pair(m_tableWidgetLineEdit.get(), &m_angleValidator));
    }
    else if (index.column() == 1)
    {
        m_validators.insert(std::make_pair(m_tableWidgetLineEdit.get(), &m_lenValidator));
    }
    m_opValidateHelper->SetValidators(m_validators);
    return TRANSFER_OWNERSHIP_TO_THIRD_PARTY(m_tableWidgetLineEdit);
}
